// mypage/userBack/userBack.js
var app=getApp();
var util=require("../../utils/util");
var config=require("../../api/config");
var my=require("../../api/my");


Page({
  /*页面的初始数据*/
  data: {
    val: '',
    img_list:[{url:''},{url:''},{url:''}],
    userinfo: ''
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    wx.getStorage({
      key: 'userinfo',
      success:function(res){
        that.setData({userinfo: res.data})
      }
    })
  },
  // 留言
  input_value:function(e){
    this.setData({val: e.detail.value})
  },
  // 上传图片
  choose_img:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    wx.chooseImage({
      count: 1,
      sourceType: ['album', 'camera'],
      success: (res) => {
        // console.log(res)
        wx.showLoading({title: '上传中'})
        that.get_oss(res,index)
      }
    })
  },
  // 提交信息
  confirm_back:function(){
    var that=this,val=that.data.val,picList=that.data.img_list,userinfo=that.data.userinfo,testSpace = /^[ ]*$/g;
    if(!val||testSpace.test(val)){
      util.page_show_toast('反馈意见不能为空',1500)
      return false;
    }
    var params={
      "phone": userinfo.phone,
      "content": util.removeAllSpace(val),
      "picList": picList
    }
    that.set_feed_back(params)
  },
  // 反馈请求
  set_feed_back:function(params){
    var that=this;
    my.set_feed_back(params).then(res=>{
      // console.log(res)
      if(res.data.code==200){
        util.page_show_toast('谢谢您的反馈意见',1500)
        that.setData({val: '',img_list:[{url:''},{url:''},{url:''}]})
        setTimeout(()=>{
          wx.navigateBack({
            delta: 1
          });
        },1500)
      }
    })
  },
  // 获取oss上传图片
  get_oss:function(file,index){
    var that=this,randomStr=util.randomString(13),img_list=that.data.img_list;
    var params={
      "isPublic": true,
      "isHttps": true
    }
    config.get_oss(params).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        wx.uploadFile({
          url: info.host,
          filePath: file.tempFilePaths[0],
          name: 'file',
          formData: {
            "key": randomStr,
            "policy": info.policy,
            "OSSAccessKeyId": info.accessKeyId,
            "signature": info.signature,
            "success_action_status": '200',

          },
          success:function(){
            wx.hideLoading()
            var img=info.host+'/'+randomStr;
            img_list[index].url=img
            that.setData({img_list: img_list})
          },
        })
      }
    })
  },
})